package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhousb.deviceManager.mapper.SysMenuMapper;
import com.zhousb.deviceManager.model.entity.SysMenu;
import com.zhousb.deviceManager.model.entity.SysRole;
import com.zhousb.deviceManager.model.vo.MenuMetaVo;
import com.zhousb.deviceManager.model.vo.MenuVo;
import com.zhousb.deviceManager.security.JwtUser;
import com.zhousb.deviceManager.service.SysMenuService;
import com.zhousb.deviceManager.utils.AuthUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZhouShenBin
 * @date 2023/12/15 16:59
 * @description
 */

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    AuthUtils authUtils;

    @Override
    public LinkedList<MenuVo> getMenuList() {
        JwtUser jwtUser = authUtils.getUser();
        Set<SysRole> roles = jwtUser.getRoles();
        List<Long> roleIds = roles.stream().map(SysRole::getId).toList();
        List<SysMenu> menuList = new ArrayList<>();
        if(authUtils.isSuperAdmin()){
            LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysMenu::getDisableFlag, 0)
                    .orderByAsc(SysMenu::getSort);
            menuList = sysMenuMapper.selectList(queryWrapper);
        }else{
            if(!CollectionUtils.isEmpty(roleIds)){
                menuList = sysMenuMapper.listRoleMenu(roleIds);
            }
        }
        LinkedList<SysMenu> menuLinkedList = menuList.stream().sorted(Comparator.comparing(SysMenu::getSort)).collect(Collectors.toCollection(LinkedList::new));
        LinkedList<MenuVo> menus = listMenusByParent(menuLinkedList, -1L);
        return menus;
    }

    private LinkedList<MenuVo> listMenusByParent(LinkedList<SysMenu> menuList, Long parent) {
        LinkedList<MenuVo> menus = menuList.stream().filter(m -> m.getParentId().equals(parent)).map(menu -> {
            MenuVo menuVo = new MenuVo();
            menuVo.setId(menu.getId());
            menuVo.setName(menu.getName());
            menuVo.setPath(menu.getPath());
            menuVo.setRedirect(menu.getRedirect());
            menuVo.setComponent(menu.getComponent());

            MenuMetaVo metaVo = new MenuMetaVo();
            metaVo.setTitle(menu.getMetaTitle());
            metaVo.setIcon(menu.getMetaIcon());
            metaVo.setFrameSrc(menu.getMetaFrameSrc());
            metaVo.setCurrentActiveMenu(menu.getMetaCurrentActiveMenu());
            if (menu.getMetaHideChildrenInMenu() != null) {
                metaVo.setHideChildrenInMenu(menu.getMetaHideChildrenInMenu() == 1);
            }
            if (menu.getMetaHideMenu() != null) {
                metaVo.setHideMenu(menu.getMetaHideMenu() == 1);
            }
            if (menu.getMetaShowMenu() != null) {
                metaVo.setShowMenu(menu.getMetaShowMenu() == 1);
            }
            if (menu.getMetaHideBreadcrumb() != null) {
                metaVo.setHideBreadcrumb(menu.getMetaHideBreadcrumb() == 1);
            }
            if (menu.getMetaIgnoreKeepAlive() != null) {
                metaVo.setIgnoreKeepAlive(menu.getMetaIgnoreKeepAlive() == 1);
            }

            menuVo.setMeta(metaVo);
            return menuVo;
        }).collect(Collectors.toCollection(LinkedList::new));

        menus.forEach(menu -> {
            LinkedList<MenuVo> children = listMenusByParent(menuList, menu.getId());
            if (!CollectionUtils.isEmpty(children)) {
                menu.setChildren(children);
            }
        });

        return menus;
    }
}